package com.techwells.learningkites.fragment;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.CourseExpandListAdapter;
import com.techwells.learningkites.cache.BaseUserCenter;
import com.techwells.learningkites.controller.LoginActivity;
import com.techwells.learningkites.controller.MainPageActivity;
import com.techwells.learningkites.controller.RegisterAcitivity;
import com.techwells.learningkites.controller.SettingUserInfoActivity;
import com.techwells.learningkites.model.MainPageViewModel;
import com.techwells.learningkites.model.RegisterViewModel;
import com.techwells.learningkites.networkservice.Constant;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.taco.mvvm.Route;
import com.techwells.taco.networkmodel.BaseUser;
import com.techwells.taco.networkservice.ServiceMediator;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.utils.Base64Coder;
import com.techwells.taco.utils.CommonUtil;
import com.techwells.taco.utils.StreamUtil;
import com.techwells.taco.utils.ToastUtils;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

public class RegisterMobileFragment extends Fragment implements OnClickListener {
	
	private EditText mobile,password,verify;
	private TextView btnRegister;
//	private Button btnGetCaptcha;
	private ImageView captcha;
	
	private RegisterAcitivity activity;
	private String captchaString;
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_register_moblie, container,false);
	}

	
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		activity = (RegisterAcitivity) getActivity();
		initViews();
	}
	
	private void initViews() {
		mobile = (EditText) getActivity().findViewById(R.id.mobile);
		verify = (EditText) getActivity().findViewById(R.id.verify);
		password = (EditText) getActivity().findViewById(R.id.rpassword);
		btnRegister = (TextView) getActivity().findViewById(R.id.btn_register2);
		btnRegister.setOnClickListener(this);
//		btnGetCaptcha = (Button) getActivity().findViewById(R.id.get_verify);
//		btnGetCaptcha.setOnClickListener(this);
		captcha = (ImageView) getActivity().findViewById(R.id.captcha);
		captcha.setOnClickListener(this);
	}


	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btn_register2:
//			Route.route().nextController(getActivity(), SettingUserInfo.class.getName(), Route.WITHOUT_RESULTCODE);
			doRequestRegisterMobile();
			break;
		case R.id.captcha:
			doRequestCaptcha();
			break;

		default:
			break;
		}
	}


	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		doRequestCaptcha();
	}
	
	
	private void doRequestRegisterMobile() {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("loginId", mobile.getText().toString());
		argMap.put("password", password.getText().toString());
		argMap.put("verifyType", Constant.verifyType);
		argMap.put("challenge", verify.getText().toString());
		activity.doTask(KitesServiceMediator.SERVICE_DO_REGISTER,argMap);
		
	}


	private void doRequestCaptcha() {
		activity.doTask(KitesServiceMediator.SERVICE_GET_CAPTCHA,null);
	}

	private void doRequestLogin() {
		String name = mobile.getText().toString();
		String ps =  password.getText().toString();
		String p = Base64Coder.encodeString(ps);
		String t = ""+System.currentTimeMillis();
		String sign = "password="+p+"t="+t+"username="+name;
		String signMd = CommonUtil.MD5Encrypt(sign);
		
		HashMap<String, String> argMap = new HashMap<String, String>();	
		argMap.put("username", name);
		argMap.put("password", p);
		argMap.put("t", t);
		argMap.put("sign", signMd);
		
		activity.doTask(KitesServiceMediator.SERVICE_DO_LOGIN,argMap);
	}

	public void refreshData(TaskToken token) {
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_CAPTCHA)) {
			captchaString = ((RegisterViewModel)activity.baseViewModel).captcha;
			byte[] decodeByte = Base64.decode(captchaString, 0);
			Bitmap bitmap = BitmapFactory.decodeByteArray(decodeByte, 0, decodeByte.length);
//			Drawable drawable =new BitmapDrawable(getResources(), bitmap);
//			btnGetCaptcha.setBackgroundDrawable(drawable);
			captcha.setImageBitmap(bitmap);
		}else if (method.equals(KitesServiceMediator.SERVICE_DO_REGISTER)) {
			int returnCode = ((RegisterViewModel)activity.baseViewModel).returnCode;
			if (returnCode == ServiceMediator.Service_Return_Success) {
				ToastUtils.show(getActivity(), "注册成功");
				doRequestLogin();
			}
		}else if (method.equals(KitesServiceMediator.SERVICE_DO_LOGIN)) {
			int loginReturnCode = ((RegisterViewModel)activity.baseViewModel).loginReturnCode;
			if (loginReturnCode == ServiceMediator.Service_Return_Success) {
//				Route.route().nextController(getActivity(), SettingUserInfoActivity.class.getName(), Route.WITHOUT_RESULTCODE);
				Route.route().nextController(getActivity(), MainPageActivity.class.getName(), Route.WITHOUT_RESULTCODE);
			}
		}
		
	}


	
}
